package com.huluobo.a2204kotlinshopping.setting

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.databinding.ActivitySettingBinding
import com.huluobo.a2204kotlinshopping.utils.ImageUtils
import com.huluobo.base.bean.UserBean
import com.huluobo.base.db.AppDBUtils
import com.huluobo.base.net.Const
import com.huluobo.base.view.BaseMVVMActivity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector

class SettingActivity : BaseMVVMActivity<ActivitySettingBinding, SettingViewModel>() {
    private lateinit var userBean: UserBean
    override fun initData() {
        viewModel.queryUserInfo(SPUtils.getInstance().getString(Const.PARAM_USERNAME))

        viewModel.queryUserInfoSuccess.observe(this) {
            userBean = it
            ImageUtils.loadImage(this, userBean.icon, dataBinding.settingUserIcon)
            dataBinding.settingNicknameEt.hint = userBean.nickname
            dataBinding.settingSexEt.hint = userBean.sex
            dataBinding.settingBirthEt.hint = userBean.birth
        }

        viewModel.queryUserInfoFailed.observe(this) {
            Log.i("TagA", "查询用户失败:$it")
        }
        viewModel.updateIconSuccess.observe(this) {
            ToastUtils.showLong(it.message)
            ImageUtils.loadImage(this, it.data.toString(), dataBinding.settingUserIcon)
        }

        viewModel.updateIconFailed.observe(this) {
            Log.i("TagA", "上传头像失败$:it")
        }

        viewModel.updateNicknameSuccess.observe(this) {
            ToastUtils.showLong(it.message)
        }

        viewModel.updateNicknameFailed.observe(this) {
            Log.i("TagA", "更新昵称失败$:it")
        }

        viewModel.updateBirthSuccess.observe(this) {
            ToastUtils.showLong(it.message)
        }

        viewModel.updateBirthFailed.observe(this) {
            Log.i("TagA", "更新生日失败$:it")
        }

        viewModel.updateSexSuccess.observe(this) {
            ToastUtils.showLong(it.message)
        }

        viewModel.updateSexFailed.observe(this) {
            Log.i("TagA", "更新性别失败$:it")
        }
    }

    override fun initView() {
        dataBinding.settingUserIcon.setOnClickListener {
            selectPic()
        }

        dataBinding.settingNicknameBtn.setOnClickListener {
            viewModel.updateNickname(
                dataBinding.settingNicknameEt.text.toString()
            )
        }

        dataBinding.settingBirthBtn.setOnClickListener {
            viewModel.updateBirth(
                dataBinding.settingBirthEt.text.toString()
            )
        }

        dataBinding.settingSexBtn.setOnClickListener {
            viewModel.updateSex(
                dataBinding.settingSexEt.text.toString()
            )
        }
    }

    private fun selectPic() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras
            val bean = bundle?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            val filePath = bean?.path
            if (filePath != null) {
                viewModel.updateIcon(filePath)
            }
        }
    }

    override fun getViewModel(): Class<SettingViewModel> {
        return SettingViewModel::class.java
    }

    override fun getLayoutId(): Int = R.layout.activity_setting
}